//
//  News24HoursViewController.m
//  item-NetEaseNews-tyw
//
//  Created by tyw on 15/10/1.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "News24HoursViewController.h"
#import "News24HoursTableView.h"
#import "NetEaseDataService.h"
#import "MJRefresh.h"

#define navImgViewHeight 260.0/828.0*kWidth        //顶部背景图片的高度

@interface News24HoursViewController ()

@end

@implementation News24HoursViewController {
    UIImageView* navImgView;            //位于顶部的背景图,100
    
    UIImageView* titleImgview;          //位于顶部的“24小时要闻”字样的图片,101
    
    UILabel* introduceLabel;            //位于顶部的“聚焦今日时事 浓缩新闻精华”字样,102
    
    UIButton* backBtn;                  //返回按钮,pop
    
    News24HoursTableView* _tableView;   //24小时要闻表视图
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self _createTableView];
    [self _createNavBar];
    
    [self _loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)_createNavBar {
#warning 这里的顶部背景和返回按钮的显示样式会随着白天晚上的切换而改变
    //判断当前是白天还是晚上
    NSDate* date = [NSDate date];
    NSString* currentHourStr = [Tools strFromDate:date withFormatterStr:@"H"];      //当前的时间的“时”
    BOOL isNight = ([currentHourStr integerValue] > 8 && [currentHourStr integerValue] < 20)?NO:YES;
    
    //01 顶部背景图
    navImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, navImgViewHeight)];
    navImgView.userInteractionEnabled = YES;
    NSString* imgName = isNight?@"todaynews_header_bg_night":@"todaynews_header_bg_day";
    navImgView.image = [UIImage imageNamed:imgName];
    navImgView.tag = 100;
    [self.view addSubview:navImgView];
    
    
    //02 顶部“24小时要闻”
    CGFloat titleImgViewHeight = 70/3*2;
    titleImgview = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/2-100, 40, 200, titleImgViewHeight)];
    titleImgview.image = [UIImage imageNamed:@"todaynews_title_bg"];
    titleImgview.tag = 101;
    [self.view addSubview:titleImgview];
    
    
    //03 聚焦今日时事。。
    introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 40+titleImgViewHeight, kWidth, 20)];
    introduceLabel.text = @"-- 聚焦今日时事 浓缩新闻精华 --";
    introduceLabel.textColor = [UIColor whiteColor];
    introduceLabel.textAlignment = NSTextAlignmentCenter;
    introduceLabel.font = [UIFont systemFontOfSize:12];
    introduceLabel.tag = 102;
    [self.view addSubview:introduceLabel];
    
    
    //04 返回按钮
    backBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, 25, 44, 33)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"icon_back_highlighted"] forState:UIControlStateSelected];
    backBtn.selected = isNight?YES:NO;
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)_createTableView {
    _tableView = [[News24HoursTableView alloc] initWithFrame:CGRectMake(0, navImgViewHeight-navBarHeight-10, kWidth, kHeight-navImgViewHeight+navBarHeight+10+tabBarHeight)];
    [self.view addSubview:_tableView];
    
    //为了表视图滑动时实现的炫酷的动画，要为他加上一个透明的头部视图
    UIView* tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, navImgViewHeight-navBarHeight)];
    tableHeaderView.backgroundColor = [UIColor clearColor];
    _tableView.tableHeaderView = tableHeaderView;
    
    //增加下拉刷新事件
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(_loadData)];
}

- (void)_loadData {
    [NetEaseDataService news24HoursDataRequestFrom:0 size:20 block:^(id result) {
        if ([result isKindOfClass:[NSError class]]) {
            NSLog(@"新闻页 24小时要闻 数据请求失败 %@", result);
            [_tableView.header endRefreshing];
            return;
        }
        NSArray* array = [result objectForKey:@"T1429173683626"];
        if (array.count) {
            [_tableView setNewsModelArray:array];
        }
        
        [_tableView.header endRefreshing];
    }];
}

@end
